package com.itheima.student;

import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/stuServlet")
public class stuServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        Score score=new Score();
        try {
            BeanUtils.populate(score,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        req.setCharacterEncoding("UTF-8");
        BufferedWriter bw =new BufferedWriter(new FileWriter("d:\\score.txt",true));
        bw.write(score.getName()+","+score.getAge()+","+score.getScore());
        bw.newLine();
        bw.flush();
        bw.close();
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("提交成功，3s后返回主页面");
        resp.setHeader("Refresh","3;URL="+req.getContextPath()+"/login.html");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
